﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument

{
    class ISYC_301_03
    {

        //图F.5  ISYC 301-03-细柄螺纹挤压丝锥
        public static void ISYC_301_03_j(Dictionary<string, Object> data)  //细柄挤压丝锥
    {
        DxfDocument dxf = new DxfDocument();
        Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
        Layer center = new Layer("center") { Color = AciColor.Yellow };    //中心线
        Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层


        double pi = Math.PI;

        double sl = Convert.ToDouble(data["oal"]);
        //double sl = 110;
        double scale;
        if (sl <= 50)
        {
            scale = 2.2;
        }
        else if (50 < sl && sl <= 75)
        {
            scale = 1.8;
        }
        else if (75 < sl && sl <= 100)
        {
            scale = 1.4;
        }
        else
        {
            scale = 1;
        }
            
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double tcpd = Convert.ToDouble(data["tcpd"]) * scale;
            double dmm = Convert.ToDouble(data["dmm"]) * scale;
            double thl = Convert.ToDouble(data["thl"]) * scale;
            double tcl = Convert.ToDouble(data["tcl"]) * scale;
            double td = Convert.ToDouble(data["td"]) * scale;
            double isframe = Convert.ToDouble(data["isframe"]);
            string path = Convert.ToString(data["path"]);
            /*
            double oal = 110 * scale;
            double tcpd = 11 * scale;
            double dmm =9 * scale;
            double thl = 23 * scale;
            double tcl =2 * scale;
            double td = 12.7 * scale;
            double isframe = 0;
            string path = Convert.ToString(data["path"]);*/
            Vector2 p0 = new Vector2(0, 0);
        Vector2 p0p = new Vector2(p0.X, p0.Y - oal);
        Vector2 p1 = fun.Polar(p0, 0, dmm / 2);
        Vector2 p2 = fun.Polar(p1, 1.5 * pi, oal - thl - (td - tcpd) / 2);
        Vector2 p3 = new Vector2(p0.X + td / 2, p0.Y - (oal - thl));
        Vector2 p4 = new Vector2(p0.X + td / 2, p0p.Y + tcl);
        Vector2 p5 = fun.Polar(p0p, 0, tcpd / 2);
        Vector2 p6 = new Vector2(p0.X - p5.X, p5.Y);
        Vector2 p7 = new Vector2(p0.X - p4.X, p4.Y);
        Vector2 p8 = new Vector2(p0.X - p3.X, p3.Y);
        Vector2 p9 = new Vector2(p0.X - p2.X, p2.Y);
        Vector2 p10 = new Vector2(p0.X - p1.X, p1.Y);

        Vector2 p20 = fun.Polar(p0, 0, dmm / 10);
        Vector2 p21 = fun.Polar(p20, 0, dmm * 0.3);
        Vector2 p22 = fun.Polar(p21, 1.5 * pi, oal / 10);
        Vector2 p23 = new Vector2(p20.X, p22.Y);
        Vector2 p24 = fun.Polar(p0, pi, dmm / 10);
        Vector2 p25 = fun.Polar(p24, pi, dmm * 0.3);
        Vector2 p26 = new Vector2(p25.X, p23.Y);
        Vector2 p27 = new Vector2(p24.X, p23.Y);

        Vector2 p30 = new Vector2(p0.X - tcpd / 6, p2.Y + tcpd / 3);
        Vector2 p31 = new Vector2(p30.X, p2.Y);
        Vector2 p32 = new Vector2(p30.X, p5.Y);
        Vector2 p33 = new Vector2(p0.X - p32.X, p32.Y);
        Vector2 p34 = new Vector2(p0.X - p31.X, p31.Y);
        Vector2 p35 = new Vector2(p0.X - p30.X, p30.Y);

        p0p = fun.rotatePoint(p0p);
        p1 = fun.rotatePoint(p1);
        p2 = fun.rotatePoint(p2);
        p3 = fun.rotatePoint(p3);
        p4 = fun.rotatePoint(p4);
        p5 = fun.rotatePoint(p5);
        p6 = fun.rotatePoint(p6);
        p7 = fun.rotatePoint(p7);
        p8 = fun.rotatePoint(p8);
        p9 = fun.rotatePoint(p9);
        p10 = fun.rotatePoint(p10);

        p20 = fun.rotatePoint(p20);
        p21 = fun.rotatePoint(p21);
        p22 = fun.rotatePoint(p22);
        p23 = fun.rotatePoint(p23);
        p24 = fun.rotatePoint(p24);
        p25 = fun.rotatePoint(p25);
        p26 = fun.rotatePoint(p26);
        p27 = fun.rotatePoint(p27);

        p30 = fun.rotatePoint(p30);
        p31 = fun.rotatePoint(p31);
        p32 = fun.rotatePoint(p32);
        p33 = fun.rotatePoint(p33);
        p34 = fun.rotatePoint(p34);
        p35 = fun.rotatePoint(p35);

        LwPolyline poly1 = new LwPolyline();
        poly1.Vertexes.Add(new LwPolylineVertex(p1));
        poly1.Vertexes.Add(new LwPolylineVertex(p2));
        poly1.Vertexes.Add(new LwPolylineVertex(p3));
        poly1.Vertexes.Add(new LwPolylineVertex(p4));
        poly1.Vertexes.Add(new LwPolylineVertex(p5));
        poly1.Vertexes.Add(new LwPolylineVertex(p6));
        poly1.Vertexes.Add(new LwPolylineVertex(p7));
        poly1.Vertexes.Add(new LwPolylineVertex(p8));
        poly1.Vertexes.Add(new LwPolylineVertex(p9));
        poly1.Vertexes.Add(new LwPolylineVertex(p10));
        poly1.Vertexes.Add(new LwPolylineVertex(p1));
        fun.PolyFill(dxf, poly1, 0.3, contour);

        Line dotline1 = new Line(fun.Polar(p0, 0, 2), fun.Polar(p0p, pi, 2));
        dotline1.Linetype = Linetype.Center;
        dotline1.Layer = center;
        dxf.AddEntity(dotline1);

        Vector2[] line3 = { p25, p26, p27, p24, p26 };
        fun.drawPline(dxf, line3, 0.15, contour);
        Vector2[] line4 = { p25, p27 };
        fun.drawPline(dxf, line4, 0.15, contour);
        Vector2[] line5 = { p21, p22, p23, p20, p22 };
        fun.drawPline(dxf, line5, 0.15, contour);
        Vector2[] line6 = { p21, p23 };
        fun.drawPline(dxf, line6, 0.15, contour);

        LwPolyline poly2 = new LwPolyline();
        poly2.Vertexes.Add(new LwPolylineVertex(p33));
        poly2.Vertexes.Add(new LwPolylineVertex(p35));
        poly2.Vertexes[1].Bulge = 1;
        poly2.Vertexes.Add(new LwPolylineVertex(p30));
        poly2.Vertexes.Add(new LwPolylineVertex(p32));
        fun.PolyFill(dxf, poly2, 0.3, contour);

        Vector2[] line7 = { p9, p31 };
        fun.drawPlineOfw(dxf, line7, 0.3, contour);
        Vector2[] line8 = { p2, p34 };
        fun.drawPlineOfw(dxf, line8, 0.3, contour);

        Vector2[] xiline1 = { p2, p5 };
        fun.drawPline(dxf, xiline1, 0.15, contour);
        Vector2[] xiline2 = { p6, p9 };
        fun.drawPline(dxf, xiline2, 0.15, contour);

        LwPolyline poly3 = new LwPolyline();
        poly3.Vertexes.Add(new LwPolylineVertex(p6));
        poly3.Vertexes.Add(new LwPolylineVertex(p7));
        poly3.Vertexes.Add(new LwPolylineVertex(p8));
        poly3.Vertexes.Add(new LwPolylineVertex(p9));
        poly3.Vertexes.Add(new LwPolylineVertex(p31));
        poly3.Vertexes.Add(new LwPolylineVertex(p32));
        poly3.IsClosed = true;

        String[] type = new String[100];
        Line[] line = new Line[100];
        int numst = poly3.Explode().Count;  //记录爆炸后，线段有多少
        for (int i = 0; i < numst; i++)
        {
            type[i] = poly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
            if (type[i] == "Line")
            {
                line[i] = (Line)poly3.Explode()[i];
            }
        }
        fun.hatchsubofline(dxf, type, numst, 85, 20, line, cutting);

        LwPolyline poly4 = new LwPolyline();
        poly4.Vertexes.Add(new LwPolylineVertex(p2));
        poly4.Vertexes.Add(new LwPolylineVertex(p3));
        poly4.Vertexes.Add(new LwPolylineVertex(p4));
        poly4.Vertexes.Add(new LwPolylineVertex(p5));
        poly4.Vertexes.Add(new LwPolylineVertex(p33));
        poly4.Vertexes.Add(new LwPolylineVertex(p34));
        poly4.IsClosed = true;

        String[] type4 = new String[100];
        Line[] pline4 = new Line[100];
        int numst4 = poly4.Explode().Count;  //记录爆炸后，线段有多少
        for (int i = 0; i < numst4; i++)
        {
            type4[i] = poly4.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
            if (type4[i] == "Line")
            {
                pline4[i] = (Line)poly4.Explode()[i];
            }
        }
        fun.hatchsubofline(dxf, type4, numst4, 85, 20, pline4, cutting);

        DimensionStyle dimensionStyle = fun.CreateDimStyle();
        dimensionStyle.ArrowSize = 2.5;
        dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
        dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
        dimensionStyle.TextHeight = 1.5;
        double offset = 20;

        LinearDimension linedim1 = new LinearDimension(new Line(p6, p10), offset * 3, 0, dimensionStyle);
        linedim1.UserText = String.Format("{0}", "OAL");
        dxf.AddEntity(linedim1);

        LinearDimension linedim2 = new LinearDimension(new Line(p6, p8), offset, 0, dimensionStyle);
            linedim2.UserText = String.Format("{0}", "THL");
        dxf.AddEntity(linedim2);

        LinearDimension linedim3 = new LinearDimension(new Line(p1, p10), offset, 270, dimensionStyle);
        linedim3.UserText = String.Format("{0}", "DCONMS");
        dxf.AddEntity(linedim3);

        LinearDimension linedim4 = new LinearDimension(new Line(p4, p5), offset, 180, dimensionStyle);
        linedim4.UserText = String.Format("{0}", "TCL");
        dxf.AddEntity(linedim4);

        LinearDimension linedim5 = new LinearDimension(new Line(p5, p6), offset, 90, dimensionStyle);
        linedim5.UserText = String.Format("{0}", "TCPD");
        dxf.AddEntity(linedim5);

        LinearDimension linedim6 = new LinearDimension(new Line(p4, p7), offset * 2, 90, dimensionStyle);
        linedim6.UserText = String.Format("{0}", "TD");
        dxf.AddEntity(linedim6);

        if (isframe == 1)
        {
            Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 210 - oal / 2, p0.Y + 148.5), data);
            fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
        }
        dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
        dxf.Save(path);
        }
    }
}


